$(function() {
    $(".update-budget-item").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (checkValidate('error-msg')) {
                $(".update-budget-item").attr("disabled", "disabled");
                if ($(this).hasClass("add-item")) {
                    item_edit_id = 0;
                } else {
                    item_edit_id = sessionStorage.budget_item_id;
                }
 
                $.post(localStorage.domain_app+"/webservice/budget/updateBudgetItem.php", {
                    item: $('input[name="Item"]').val(),
                    amount: $('input[name="Amount"]').val(),                 
                    user_id: localStorage.userID,
                    item_id: item_edit_id,
                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;

                    } else {
                         print_alert(res.msg);
                       
                    }
                    if (msgEr === '') {
                        wd_redirect("budget.html");

                    } else {
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    $(".update-budget-item").removeAttr("disabled");
                }, "json");
            }
        } else {
            return false;
        }
    });
    //paging
//    var killScroll = false;
//    $(window).scroll(function() {
//        if ($('#end_paging').val() == 0) {
//            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
//                $('.wd-loading').show();
//                if (killScroll == false) {
//                    killScroll = true;
//                    $.post(localStorage.domain_app+"/webservice/event/index.php", {
//                        page: $('#page').val(),
//                        user_id: localStorage.userID
//                     }, function(res) {
//                        $('.wd-list-event').append(res.html);
//                        $('#page').val(res.page);                    
//                        $('#end_paging').val(res.end_paging);
//                        saveEventID();
//                        deleteEvent();
//                        killScroll = false;
//                        $('.wd-loading').hide();
//                    }, "json");
//                } else {
//                    return false;
//                }
//            }
//        }
//    });
    $('.input_text').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    
});
function checkValidate(id_append) {
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please enter %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="Item"]').val() == '' || $('input[name="Item"]').val() == 'Item name*') {
        msgEr = errormsg[2].replace('%s', 'item name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="Item"]'), num_milliseconds);
        return false;
    }

    if ($('input[name="Amount"]').val() == '' || $('input[name="Amount"]').val() == 'Amount*') {
        msgEr = errormsg[2].replace('%s', 'amount');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="Amount"]'), num_milliseconds);
        return false;
    }
    else {
        if(!isNumber($('input[name="Amount"]').val())){
             msgEr = "Please enter a valid amount.";
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="Amount"]'), num_milliseconds);
            return false;
        }
        

    }
   
    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append,num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}